#include <stdio.h>
#include "simpio.h"
#include "genlib.h"
#include "strlib.h"


void PrintGrayCode(int nBits);
void gray0(string gray, int nBits);
void gray1(string gray, int nBits);

main(){
	int nBits;
	while(TRUE){
		printf("Enter number of Bits (enter 0 to exit): ");
		nBits = GetInteger();
		if(nBits > 0)
			PrintGrayCode(nBits);
		if(nBits == 0)
			break;
	}
}

void PrintGrayCode(int nBits){
	gray0("", nBits);
}

void gray0(string gray, int nBits){
	if(nBits == 0)
		printf("%s\n",gray);
	else{
		gray0(Concat(gray,"0"),nBits-1);
		gray1(Concat(gray,"1"),nBits-1);
	}
}

void gray1(string gray, int nBits){
	if(nBits == 0)
		printf("%s\n",gray);
	else{
		gray0(Concat(gray,"1"),nBits-1);
		gray1(Concat(gray,"0"),nBits-1);
	}
}